package com.imay.photopicker.activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.imay.capturefilter.R;
import com.imay.photopicker.util.ViewUtil;
import com.imay.photopicker.view.TitleBar;


/**
 * Created by jeaman on 2017/2/14.
 */

public abstract class BaseActivity extends FragmentActivity {
    private View mContentView;  //除标题栏外的view
    private TitleBar mTitleBar; //标题栏

    protected boolean mResumed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
//            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
//        }
        int layoutId = getLayoutId();
        if (layoutId > 0) {
            setContentView(layoutId);
            initView(savedInstanceState);
            findScrollView();
        }
    }

    @Override
    final public void setContentView(@LayoutRes int layoutResID) {
        mContentView = inflate(layoutResID, null);
        if (useDynamicTitleBar()) { //动态添加标题栏
            View titleBar = getCustomTitleBar();
            if (titleBar == null) {
                mTitleBar = new TitleBar(this);
                mTitleBar.setBackgroundResource(R.color.bg_title_bar);
                titleBar = mTitleBar;
                if (useLeftDefaultIcon()) {
                    //增加返回按钮
                    mTitleBar.setLeftImageResource(R.mipmap.btn_03_camera_close);
                    mTitleBar.setLeftImageOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                }
            }

            titleBar.setFitsSystemWindows(true);
            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            //添加标题栏
            linearLayout.addView(titleBar, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            //添加view
            linearLayout.addView(mContentView, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            super.setContentView(linearLayout);
        } else {
            super.setContentView(layoutResID);
        }
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 设置布局,如果子类不想设置，返回0就可以
     */
    protected abstract @LayoutRes int getLayoutId();

    /**
     * 初始化控件
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * 默认使用动态的titlebar，如果特殊情况不使用，需复写此方法，并返回false
     */
    protected boolean useDynamicTitleBar() {
        return true;
    }

    /**
     * 默认使用titlebar的左侧关闭按钮
     */
    protected boolean useLeftDefaultIcon() {
        return true;
    }

    protected boolean autoFindScrollView() {
        return true;
    }

    /**
     * 复写此方法，返回自定义的titlebar
     *
     * @return 返回自定义的titleBar
     */
    protected View getCustomTitleBar() {
        return null;
    }

    /**
     * 动态加载布局文件
     *
     * @param layoutResID
     * @param root
     * @return
     */
    protected View inflate(int layoutResID, ViewGroup root) {
        return LayoutInflater.from(this).inflate(layoutResID, root);
    }

    /**
     * 查找控件
     *
     * @param id
     * @return
     */
    protected <T extends View> T $(int id) {
        return (T) findViewById(id);
    }

    protected <T extends View> T $(int id, View view) {
        return (T) view.findViewById(id);
    }

    protected View getContentView() {
        return mContentView;
    }

    protected TitleBar getTitleBar() {
        return mTitleBar;
    }

    protected void setTitleBar(TitleBar titleBar) {
        mTitleBar = titleBar;
    }

    /**
     * 设置title
     * 如果不是用默认的TitleBar，不能设置此属性
     * @param title
     */
    protected void setTitle(String title) {
        if (mTitleBar != null) {
            mTitleBar.setTitle(title);
        } else {
            throw new IllegalStateException("你没有使用默认的TitleBar，不能使用此方法设置标题");
        }
    }

    /**
     * 遍历找到viewGroup下面的第一个可滑动的View
     */
    private void findScrollView() {
        if (autoFindScrollView() && useDynamicTitleBar()) {
            View canScrollView = ViewUtil.findFirstCanScrollView((ViewGroup) getContentView());
            if (canScrollView != null) {
                setSmoothToTopView(canScrollView);
            }
        }
    }

    public void setSmoothToTopView(View canScrollView) {
        if (mTitleBar != null) {
            mTitleBar.setSmoothToTopView(canScrollView);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mResumed = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mResumed = true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //不继承超类,则不保存fragment
//     super.onSaveInstanceState(outState);
    }
}
